package com.recycle.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.recycle.common.constant.CacheConstants;
import com.recycle.common.exceptions.CaptchaException;
import com.recycle.common.exceptions.CaptchaExpireException;
import com.recycle.common.utils.RedisOperator;
import com.recycle.mapper.SysUserMapper;
import com.recycle.pojo.SysUser;
import com.recycle.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 梦魂
 * @since 2025-08-13
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    RedisOperator redisOperator;

    // 验证码开关
    @Value("${captcha.enabled}")
    String captchaEnabled;


    /**
     * 校验验证码
     *
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    public void validateCaptcha(String code, String uuid) throws CaptchaExpireException, CaptchaException {
        if ("true".equals(captchaEnabled))
        {
            String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
            String captcha = redisOperator.get(verifyKey);
            if (captcha == null)
            {
                throw new CaptchaExpireException();
            }
            redisOperator.del(verifyKey);
            if (!code.equalsIgnoreCase(captcha))
            {
                throw new CaptchaException();
            }
        }
    }

}
